package applet.molecule;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JToggleButton;

@SuppressWarnings("serial")
public class DspButton extends JToggleButton {
	
	public static final int SIZE = DspPanel.SIZE - 12;
	
	private static final int RADIUS = 4;
	
	private List<Atom> atomList;
	
	private List<Atom> lineList;
	
	private int index;
	
	private float centerX;
	private float centerY;
	
	private float scale;
	
	private DspButton(){
		lineList = new ArrayList<Atom>();
	}
	
	public DspButton(int index){
		this();
		
		this.index = index;
	}

	public DspButton(int index, List<Atom> list){
		this(index);
		
		setAtomList(list);
	}
	
	public int getIndex(){
		return index;
	}
	
	public void setAtomList(List<Atom> atomList){
		lineList.clear();
		
		this.atomList = atomList;
		
		// extreme number
		float maxX = -999999, minX = 999999;
		float maxY = -999999, minY = 999999;
		
		float totalX = 0;
		float totalY = 0;
		
		float x, y;
		for(Atom a: atomList){
			x = a.x;			
			y = a.y;
			
			totalX += x;
			totalY += y;
			
			if(maxX < x){
				maxX = x;
			}
			if(maxY < y){
				maxY = y;
			}
			if(minX > x){
				minX = x;
			}
			if(minY > y){
				minY = y;
			}
			
			if(a.element.equals("C") || a.element.equals("HO")){
				lineList.add(a);
			}
		}
		
		centerX = totalX/atomList.size();
		centerY = totalY/atomList.size();

		float max1 = (maxX-centerX > maxY-centerY)? maxX-centerX: maxY-centerY;
		float max2 = (centerX-minX > centerY-minY)? centerX-minX: centerY-minY;
		float max = (max1 > max2)? max1: max2;
		
		scale = (SIZE/2 - RADIUS - 4)/max;
	}
	
	public List<Atom> getList(){
		return atomList;
	}
	
	public void paint(Graphics g){
		super.paint(g);

		final int x = SIZE/2 - RADIUS;
		final int y = SIZE/2 - RADIUS;

		Atom a = null;
		int offset = 0;
		
		int size = atomList.size();
		for(int i = 0; i<size; i++){
			a = atomList.get(i);
			
			if(a.element.equals("H")){
				g.setColor(Color.WHITE);
			}else if(a.element.equals("HO")){
				drawLineIfNeeded(offset++, g);
				
				g.setColor(Color.BLUE);
			}else if(a.element.equals("WO")){
				g.setColor(Color.RED);
			}else{
				drawLineIfNeeded(offset++, g);
				
				continue;
			}
			
			g.fillOval(x + (int)((a.x-centerX)*scale), y - (int)((a.y-centerY)*scale), RADIUS*2, RADIUS*2);
		}
	}
	
	private void drawLineIfNeeded(int offset, Graphics g){
		Atom a = lineList.get(offset);
		Atom temp = null;
		for(int i = 0; i<offset; i++){
			temp = lineList.get(i);
			if(Math.sqrt((a.x-temp.x)*(a.x-temp.x) + (a.y-temp.y)*(a.y-temp.y)
					+ (a.z-temp.z)*(a.z-temp.z)) < Attribute.PIPE_THRES_C){
				g.setColor(Color.GREEN);

				g.drawLine(SIZE/2 + (int) ((a.x - centerX) * scale), SIZE/2
						- (int) ((a.y - centerY) * scale), SIZE/2
						+ (int) ((temp.x - centerX) * scale), SIZE/2
						- (int) ((temp.y - centerY) * scale));
			}
		}
	}
}
